﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml;

namespace AutoEditUI.UserControls
{
    /// <summary>
    /// ImageProp.xaml 的交互逻辑
    /// </summary>
    public partial class ImageProp : UserControl
    {
        public ImageProp()
        {
            InitializeComponent();
        }

        public Image gx { get; private set; }
        public UIItem gUIItem { get; private set; }

        internal void Bind(MainWindow mainWindow, Image x)
        {
            gx = x;
            gUIItem = (UIItem)mainWindow.GeItemByTag((string)x.Tag);
            localrelate.SetItm(mainWindow,gUIItem, x);
            TextBlockTag.Text = x.Tag.ToString();
            TextBoxW.Text = x.Width.ToString();
            TextBoxH.Text = x.Height.ToString();
            //TextBoxSource.Text = x.Source.ToString();
         //   CheckBoxXY.IsChecked = gUIItem.BXY;

            x.SetBinding(Image.WidthProperty, new Binding("Text") { Source = TextBoxW });
            x.SetBinding(Image.HeightProperty, new Binding("Text") { Source = TextBoxH });
           // x.SetBinding(Image.SourceProperty, new Binding("Text") { Source = TextBoxSource });

            X.Text = x.GetValue(Canvas.LeftProperty).ToString();
            Y.Text = x.GetValue(Canvas.TopProperty).ToString();

            Binding xxx = new Binding("Text") { Source = X };
            xxx.Mode = BindingMode.TwoWay;
            x.SetBinding(Canvas.LeftProperty, xxx);

            Binding yyy = new Binding("Text") { Source = Y };
            yyy.Mode = BindingMode.TwoWay;
            x.SetBinding(Canvas.TopProperty, yyy);

            TextBoxName.Text = x.Name.ToString();
            x.SetBinding(Image.NameProperty, new Binding("Text") { Source = TextBoxName });
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var openFileDialog = new Microsoft.Win32.OpenFileDialog()
            {
                Filter = "PNG Files (*.png)|*.PNG"
            };
            var result = openFileDialog.ShowDialog();
            if (result == true)
            {
               // BitmapImage image = 

                gx.Source = new BitmapImage(new Uri(openFileDialog.FileName, UriKind.Absolute));

                //string Pathx = AppDomain.CurrentDomain.BaseDirectory + @"\Icons\" + openFileDialog.SafeFileName;
                //if (!Directory.Exists(AppDomain.CurrentDomain.BaseDirectory + @"\Icons"))//如果不存在就创建 dir 文件夹  
                //    Directory.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory + @"\Icons");

                //if (openFileDialog.FileName!= Pathx && !File.Exists(AppDomain.CurrentDomain.BaseDirectory + @"\Icons\" + openFileDialog.SafeFileName) ) {
                //    File.Copy(openFileDialog.FileName, AppDomain.CurrentDomain.BaseDirectory + @"\Icons\" + openFileDialog.SafeFileName);
                //}
                //  TextBoxSource.Text = AppDomain.CurrentDomain.BaseDirectory + @"\Icons\" + openFileDialog.SafeFileName;

            }
        }

     
        
    }
}
